#include "StdAfx.h"
#include "ImageBadPixelRevise.h"

ImageBadPixelRevise::ImageBadPixelRevise(void)
{
	m_nWidth  = 0;    
	m_nHeight = 0;   

	m_bBadPixelReviseImage    = FALSE;
	m_bOldBadPixelReviseImage = FALSE;

	m_nDataSize = 0;
	m_pwData    = NULL;
}

ImageBadPixelRevise::~ImageBadPixelRevise(void)
{
	m_nWidth  = 0;    
	m_nHeight = 0; 

	m_nDataSize = 0;
	MySafeDeleteMAB(m_pwData);

	UnInitReviseData();
}


int ImageBadPixelRevise::SelectBadPixelReviseFile(char *pFile)
{
	if( NULL == pFile )return -1;

	m_nWidth  = 0;    
	m_nHeight = 0; 


	int nRet = -1;

	m_bBadPixelReviseImage = FALSE;

	WORD *pwData = NULL;

	do 
	{
		DDReadWriteFile xFile;

		if( 0 != xFile.DDCreateFile(pFile) )
		{
			nRet = -1;
			break;
		}

		nRet = xFile.DDOperatorFile( (BYTE*)&m_xBadPixelResviseFile,sizeof(zkBadPixelReviseFileInfo) );
		if( nRet != 0)
		{
			break;
		}

		m_nWidth  = m_xBadPixelResviseFile.nWidth; 
		m_nHeight = m_xBadPixelResviseFile.nHeight;

		int nSize = m_nWidth * m_nHeight;

		pwData = new WORD[nSize+10];
		if( NULL == pwData )
		{
			break;
		}

		nRet = xFile.DDOperatorFile( (BYTE*)pwData,sizeof(WORD)*nSize);
		if( nRet != 0)
		{
			break;
		}
		
		m_pwData = pwData;
	
		m_bBadPixelReviseImage = TRUE;

		return 0;

	}while(false);

	MySafeDeleteMAB(pwData);
	
	return nRet;
}

inline WORD GetValue(WORD *pBadData,WORD *pData,int nOffset,int nW)
{
	DWORD dwValue=0;
	DWORD dwCount=0;

	int n1 = nOffset - nW -1;
	int n2 = nOffset - nW   ;
	int n3 = nOffset - nW +1;
	int n4 = nOffset      -1;
	int n5 = nOffset      +1;
	int n6 = nOffset + nW -1;
	int n7 = nOffset + nW   ;
	int n8 = nOffset + nW +1;

	if( pBadData[n1] == 0 ){dwValue += pData[n1];dwCount ++;}
	if( pBadData[n2] == 0 ){dwValue += pData[n2];dwCount ++;}
	if( pBadData[n3] == 0 ){dwValue += pData[n3];dwCount ++;}
	if( pBadData[n4] == 0 ){dwValue += pData[n4];dwCount ++;}
	if( pBadData[n5] == 0 ){dwValue += pData[n5];dwCount ++;}
	if( pBadData[n6] == 0 ){dwValue += pData[n6];dwCount ++;}
	if( pBadData[n7] == 0 ){dwValue += pData[n7];dwCount ++;}
	if( pBadData[n8] == 0 ){dwValue += pData[n8];dwCount ++;}

	if( dwCount > 1 )
	{
		dwValue /= dwCount;
	}
	else
	{
		dwValue = 0;
		dwValue += pData[n1];
		dwValue += pData[n2];
		dwValue += pData[n3];
		dwValue += pData[n4];

		dwValue >>= 2;
	}

	return (WORD)dwValue;
}

int ImageBadPixelRevise::BadPixelRevise(zkImgBitInfo *pImgBitInfo)
{
	if( !m_bBadPixelReviseImage ) return -1;
	if( NULL == pImgBitInfo ) return -1;

	if( NULL == m_pwData   )return -1;

	switch( pImgBitInfo->nImgDefine ) 
	{
	case eImage_Define_Img8:
		{

		}
		break;
	case eImage_Define_Img16:
		{
			int nWidth  = pImgBitInfo->nWidth;
			int nHeight = pImgBitInfo->nHeight;
			
			if( nWidth != m_nWidth || nHeight != m_nHeight )
			{
				return -1;
			}

			WORD *pBadData = m_pwData;
			WORD *pData =  (WORD*)pImgBitInfo->pImageData;

			int nLeft   = nWidth -1;
			int nBottom = nHeight -1;

			int nOffset = 0;
			int nValue  = 0;
			for(int i=1;i<nBottom;i++)
			{
				for(int j=1;j<nLeft;j++)
				{
					nOffset = i*nWidth+j;

					if( pBadData[nOffset] > 0 )
					{    
						pData[nOffset] = GetValue(pBadData,pData,nOffset,nWidth);                       
					}	
				}
			}
		}
		break;
	case eImage_Define_Img32:
		{
		}
		break;
	default:
		break;
	}


	return 0;
}



void ImageBadPixelRevise::InitReviseData()
{
}

void ImageBadPixelRevise::UnInitReviseData()
{
	
}


int ImageBadPixelRevise::SetBadPixelGradeOne(zkImgBitInfo *pImgBitInfo)
{
	if( NULL == pImgBitInfo ) return -1;

	switch( pImgBitInfo->nImgDefine ) 
	{
	case eImage_Define_Img8:
		{
			
		}
		break;
	case eImage_Define_Img16:
		{
			int nWidth  = pImgBitInfo->nWidth;
			int nHeight = pImgBitInfo->nHeight;
			m_nWidth  = nWidth; 
			m_nHeight = nHeight;	

			int nSize = nWidth * nHeight;
			if( m_nDataSize < nSize )
			{
				MySafeDeleteMAB(m_pwData);
				m_nDataSize = nSize;

				m_pwData = new WORD[nSize+10];
			}
			if( NULL == m_pwData )
			{
				return -1;
			}

			WORD *pBadData = m_pwData;
			memset(pBadData,0,sizeof(WORD)*nSize);
			WORD *pData = (WORD*)pImgBitInfo->pImageData;
			int  nMax = m_xBadPixelResviseFile.xBadReviseParam.nPixel1;

            for(int i=0;i<nSize;i++)
			{
				if( pData[i] > nMax )
				{
                    pBadData[i] = 0xffff;
				}
			}
		}
		break;
	case eImage_Define_Img32:
		{
		}
		break;
	default:
		break;
	}


	return 0;
}

int ImageBadPixelRevise::SetBadPixelGradeTwo(zkImgBitInfo *pImgBitInfo)
{
	if( NULL == pImgBitInfo ) return -1;

	switch( pImgBitInfo->nImgDefine ) 
	{
	case eImage_Define_Img8:
		{

		}
		break;
	case eImage_Define_Img16:
		{
			int nWidth  = pImgBitInfo->nWidth;
			int nHeight = pImgBitInfo->nHeight;

			if( m_nWidth != nWidth || m_nHeight != nHeight )
			{
				return -1;
			}

			int nSize = nWidth * nHeight;
			if( NULL == m_pwData )
			{
				return -1;
			}

			WORD *pBadData = m_pwData;
			WORD *pData = (WORD*)pImgBitInfo->pImageData;
			int  nMin = m_xBadPixelResviseFile.xBadReviseParam.nPixel2;

			for(int i=0;i<nSize;i++)
			{
				if( pData[i] < nMin )
				{
					pBadData[i] = 0xffff;
				}
			}
		}
		break;
	case eImage_Define_Img32:
		{
		}
		break;
	default:
		break;
	}

	return 0;
}

int ImageBadPixelRevise::SaveBadPixelReviseFile(char *pFileName)
{
	if( NULL == pFileName )
	{
		return -1;
	}

	int nRet = -1;

	do 
	{
		if( NULL == m_pwData )
		{
			return -1;
		}

		int nSize = m_nWidth * m_nHeight;

		m_xBadPixelResviseFile.nWidth  = m_nWidth;
		m_xBadPixelResviseFile.nHeight = m_nHeight;

		DDReadWriteFile xFile;

		if( 0 != xFile.DDCreateFile(pFileName,FALSE) )
		{
			nRet = -1;
			break;
		}

		nRet = xFile.DDOperatorFile( (BYTE*)&m_xBadPixelResviseFile,sizeof(zkBadPixelReviseFileInfo),NULL,FALSE);
		if( nRet != 0)
		{
			break;
		}
		nRet = xFile.DDOperatorFile( (BYTE*)m_pwData,sizeof(WORD)*nSize,NULL,FALSE);
		if( nRet != 0)
		{
			break;
		}

		UnInitReviseData();

	}while(false);


	return nRet;
}

int ImageBadPixelRevise::GetBadPixelValue(int x,int y,DWORD &dwValue)
{
	if( NULL == m_pwData )
	{
		return -1;
	}
	if( x < 0 || x >= m_nWidth ||
		y < 0 || y >= m_nHeight )
	{
		return -1;
	}

	WORD *pBadData = m_pwData;
	if( pBadData[y*m_nWidth+x] > 0 )
	{
		dwValue  = 1;
	}
	else
	{
		dwValue = 0;
	}

	return 0;
}

int ImageBadPixelRevise::SetBadPixelValue(int x,int y,DWORD dwValue)
{
	if( NULL == m_pwData )
	{
		return -1;
	}
	if( x < 0 || x >= m_nWidth ||
		y < 0 || y >= m_nHeight )
	{
		return -1;
	}

	WORD *pBadData = m_pwData;
	if( dwValue > 0 )
	{
		pBadData[y*m_nWidth+x] = 0xffff;
	}
	else
	{
		pBadData[y*m_nWidth+x] = 0;
	}

	return 0;
}